package com.neusoft.elmboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.elmboot.po.dto.PointTransactionDTO;
import com.neusoft.elmboot.po.point.PointTransaction;
import com.neusoft.elmboot.po.vo.PointTransactionVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PointTransactionMapper extends BaseMapper<PointTransaction> {

    @Insert("insert into elm.pointtransaction " +
            "(pt_id, user_id, channel_id, event_id, value, create_time, expired_time, state) " +
            "values " +
            "(#{ptId}, #{userId}, #{channelId}, #{eventId}, #{value}, #{createTime}, #{expiredTime}, #{state})")
    void addTrans(PointTransaction transaction);

    @Select("select user_id, channel_id, value, state, create_time, expired_time " +
            "from elm.pointtransaction where user_id = #{userId}")
    List<PointTransactionVO> getAllTrans(String userId);

    @Select("select user_id, channel_id, value, state, create_time, expired_time " +
            "from elm.pointtransaction where user_id = #{userId} and value < 0")
    List<PointTransactionVO> getExchangeTrans(String userId);

    @Select("select user_id, channel_id, value, state, create_time, expired_time " +
            "from elm.pointtransaction where user_id = #{userId} and value > 0")
    List<PointTransactionVO> getIncomeTrans(String userId);

    @Select("select pt_id, user_id, state, expired_time " +
            "from elm.pointtransaction " +
            "where user_id = #{userId} and state > 0 and state is not null and expired_time > NOW() " +
            "order by expired_time")
    List<PointTransactionDTO> getValidTrans(String userId);

    @Update("update elm.pointtransaction set state = #{value} where pt_id = #{ptId}")
    void updateState(Integer value, String ptId);

    @Delete("delete from elm.pointtransaction where state = -1")
    void delExpiredTrans();

    @Update("update elm.pointtransaction set state = -1 where expired_time >= NOW()")
    void setTransExpired();
}
